from flask import Blueprint, request, jsonify
from flask_jwt_extended import jwt_required
from model import Course, Teacher, CourseNotice,StudentCourse,TeacherCourse,User  # Make sure to import your models
from extension import db  # Assuming you're using Flask-SQLAlchemy
from views.user import student_required,admin_required,assistant_required

course_bp = Blueprint('course', __name__)


@course_bp.route('/get_courses/<student_id>', methods=['GET'])
@jwt_required()
@student_required
def get_courses(student_id):
    student_courses = StudentCourse.query.filter_by(student_id=student_id).all()
    if not student_courses:
        return jsonify([]), 200

    courses = []
    for student_course in student_courses:
        course = Course.query.filter_by(course_id=student_course.course_id).first()
        teacherCourse = TeacherCourse.query.filter_by(course_id=course.course_id).first()
        teacher = Teacher.query.filter_by(teacher_id=teacherCourse.teacher_id).first()
        if course:
            courses.append({
                'course_id': course.course_id,
                'course_sequence': course.course_sequence,
                'course_name': course.course_name,
                'teacher_id': teacherCourse.teacher_id,
                'teacher_name': teacher.name,
            })

    return jsonify(courses), 200


@course_bp.route('/introduction/<course_id>', methods=['POST'])
@jwt_required()
@admin_required
def upload_introduction(course_id):
    data = request.json
    introduction = data['introduction']

    print(introduction)
    # Update course introduction in database (assuming Course has an introduction field)
    course = Course.query.filter_by(course_id=course_id).first()
    if course:
        course.introduction = introduction
        db.session.commit()
        return jsonify({'message': 'Course introduction updated successfully.'}), 200
    else:
        return jsonify({'message': 'Course not found.'}), 404


@course_bp.route('/syllabus/<course_id>', methods=['POST'])
@jwt_required()
@admin_required
def upload_syllabus(course_id):
    data = request.json
    syllabus = data.get('syllabus', '')

    # Update course syllabus in database (assuming Course has a syllabus field)
    course = Course.query.filter_by(course_id=course_id).first()
    if course:
        course.syllabus = syllabus
        db.session.commit()
        return jsonify({'message': 'Syllabus updated successfully.'}), 200
    else:
        return jsonify({'message': 'Course not found.'}), 404


@course_bp.route('/calendar/<course_id>', methods=['POST'])
@jwt_required()
@admin_required
def upload_calendar(course_id):
    data = request.json
    calendar = data.get('calendar', '')

    # Update course calendar in database (assuming Course has a calendar field)
    course = Course.query.filter_by(course_id=course_id).first()
    if course:
        course.calendar = calendar
        db.session.commit()
        return jsonify({'message': 'Calendar updated successfully.'}), 200
    else:
        return jsonify({'message': 'Course not found.'}), 404


@course_bp.route('/teacher/info/<course_id>', methods=['GET'])
@jwt_required()
def get_teacher_info(course_id):
    # Get the course and associated teacher info
    course = Course.query.filter_by(course_id=course_id).first()
    if course:
        teacher = Teacher.query.filter_by(teacher_id=course.teacher_id).first()
        if teacher:
            teacher_info = {
                'teacher_id': teacher.teacher_id,
                'name': teacher.name,
                'password': teacher.password  # Be cautious not to expose sensitive info
            }
            return jsonify(teacher_info), 200
        else:
            return jsonify({'message': 'Teacher not found.'}), 404
    else:
        return jsonify({'message': 'Course not found.'}), 404


@course_bp.route('/getintro/<course_id>', methods=['GET'])
@jwt_required()
def get_course_introduction(course_id):
    course = Course.query.filter_by(course_id=course_id).first()
    if course:
        return jsonify({'introduction': course.introduction}), 200
    else:
        return jsonify({'message': 'Course not found.'}), 404


@course_bp.route('/getsyl/<course_id>', methods=['GET'])
@jwt_required()
def get_course_syllabus(course_id):
    course = Course.query.filter_by(course_id=course_id).first()
    if course:
        return jsonify({'syllabus': course.syllabus}), 200
    else:
        return jsonify({'message': 'Course not found.'}), 404


@course_bp.route('/getcal/<course_id>', methods=['GET'])
@jwt_required()
def get_course_calendar(course_id):
    course = Course.query.filter_by(course_id=course_id).first()
    if course:
        return jsonify({'calendar': course.calendar}), 200
    else:
        return jsonify({'message': 'Course not found.'}), 404


@course_bp.route('/get_students_list/<course_id>', methods=['GET'])
@jwt_required()
@admin_required
def get_students_in_course(course_id):
    # 查询该课程下的所有学生
    student_courses = StudentCourse.query.filter_by(course_id=course_id).all()
    if not student_courses:
        return jsonify({'message': 'No students found for this course.'}), 404

    student_ids = [sc.student_id for sc in student_courses]
    students = User.query.filter(User.student_id.in_(student_ids)).all()

    student_list = [{
        'student_id': student.student_id,
        'name': student.name,
    } for student in students]

    return jsonify(student_list), 200



